package com.example.zhangwei.ui.bindservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.example.zhangwei.R;
import com.example.zhangwei.service.MyBindService;
import com.example.zhangwei.service.MyBindService.SimpleBinder;
import com.example.zhangwei.util.BaseActivity;
import com.example.zhangwei.util.ToastMaster;
import com.example.zhangwei.widgts.MyPopUpWindow;

public class BindActivity extends BaseActivity{
	
	private MyPopUpWindow popupwindow;
	
	private ServiceConnection mServiceConn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_bind);
		
		popupwindow = new MyPopUpWindow(this);
		
		mServiceConn = new ServiceConnection() {
			
			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
//				if (mServiceConn != null)
//				{
//					startService(new Intent(BindActivity.this, MyBindService.class));
//			        bindService(new Intent(BindActivity.this, MyBindService.class), mServiceConn, Activity.BIND_AUTO_CREATE);
//				}
				
			}
			
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				// TODO Auto-generated method stub
				SimpleBinder localBinder = (SimpleBinder) service;
				MyBindService ser = (MyBindService) localBinder.getService();
				ToastMaster.getInstance().makeText(BindActivity.this, ser.getMessage()+"|"+localBinder.add(3, 5), Toast.LENGTH_LONG);
			}
		};
		
		findViewById(R.id.activity_living_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 初始化
				startService(new Intent(BindActivity.this, MyBindService.class));
		        bindService(new Intent(BindActivity.this, MyBindService.class), mServiceConn, Activity.BIND_AUTO_CREATE);
			}
		});
		
		findViewById(R.id.activity_living_button2).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 初始化
//				unbindService(mServiceConn);
				stopService(new Intent(BindActivity.this,MyBindService.class));
			}
		});
		
		final View view = findViewById(R.id.activity_living_popupwindow);
		
		findViewById(R.id.activity_living_popupwindow).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 初始化
				popupwindow.show(true, view);
			}
		});

	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
//		unbindService(mServiceConn);
		stopService(new Intent(BindActivity.this,MyBindService.class));
		mServiceConn = null;
		super.onDestroy();
	}

}
